//
//  ViewController.m
//  10606
//
//  Created by student on 16/3/5.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

// 定义之后可以不用带mas_前缀
#define MAS_SHORTHAND
// 定义之后equalTo等于mas_equalTo
#define MAS_SHORTHAND_GLOBALS

#import "Masonry.h"
@interface ViewController ()

@property (nonatomic,weak) UIView *greenView;

@property (nonatomic,weak) UIView *yellowView;
@property (nonatomic,weak) UIView *redView;
@property (nonatomic,weak) UIView *blueView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self test1];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)test1{
    self.greenView = [self setViewWithText:@"Ray" WithImage:@"1" WithColor:[UIColor greenColor]];
    
    [self.greenView makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view.width).multipliedBy(0.5);
        make.height.equalTo(self.view.height).multipliedBy(0.5);
        make.left.equalTo(self.view.left);
        make.top.equalTo(self.view.top);
    }];
    
    self.yellowView = [self setViewWithText:@"Ray" WithImage:@"2" WithColor:[UIColor yellowColor]];
    [self.yellowView makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view.width).multipliedBy(0.5);
        make.height.equalTo(self.view.height).multipliedBy(0.5);
        make.right.equalTo(self.view.right);
        make.top.equalTo(self.view.top);
    }];
    
    self.redView = [self setViewWithText:@"Ray" WithImage:@"3" WithColor:[UIColor redColor]];
    [self.redView makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view.width).multipliedBy(0.5);
        make.height.equalTo(self.view.height).multipliedBy(0.5);
        make.left.equalTo(self.view.left);
        make.bottom.equalTo(self.view.bottom);
    }];
    
    self.blueView = [self setViewWithText:@"Ray" WithImage:@"4" WithColor:[UIColor blueColor]];
    [self.blueView makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view.width).multipliedBy(0.5);
        make.height.equalTo(self.view.height).multipliedBy(0.5);
        make.right.equalTo(self.view.right);
        make.bottom.equalTo(self.view.bottom);
    }];
}
-(UIView *)setViewWithText:(NSString *)text
              WithImage:(NSString *)imageName
             WithColor:(UIColor *)color{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = color;
    
    UILabel *label = [[UILabel alloc] init];
    label.text = text;
    label.textAlignment = NSTextAlignmentCenter;
    [view addSubview:label];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    [imageView setImage:[UIImage imageNamed:imageName]];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [view addSubview:imageView];
    
    [label makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@25);
        make.bottom.equalTo(view.bottom).offset(-10);
        make.left.equalTo(view.left);
        make.right.equalTo(view.right);
    }];
    
    [imageView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.top).offset(20);
        make.left.equalTo(view.left).offset(20);
        make.right.equalTo(view.right).offset(-20);
        make.bottom.equalTo(label.top).offset(-20);
        
    }];
    [self.view addSubview:view];
    return view;
}
@end
